<?php
/**
* @file $Id: Top.php 543 2007-06-03 22:02:50Z focus-sis $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

// FROM Side.php --
$old_syear  = UserSyear();
$old_school = UserSchool();
$old_period = UserCoursePeriod();

if (($_REQUEST['side_school'] 
 &&  $_REQUEST['side_school'] != UserSchool() ) 
|| ( $_REQUEST['side_period'] 
 &&  $_REQUEST['side_period'] != $old_period )
){
	unset($_SESSION['student_id']);
	$_SESSION['unset_student'] = true;
	unset($_SESSION['staff_id']);
}

if ($_REQUEST['side_school'] 
&&  $_REQUEST['side_school'] != UserSchool()
){
	unset($_SESSION['UserMP']);
	unset($_REQUEST['mp']);
}

if ($_POST) {
	if (User('PROFILE') == 'admin' 
	&& $_REQUEST['side_school'] 
	&& $old_school != $_REQUEST['side_school']
	){
		$_SESSION['UserSchool'] = $_REQUEST['side_school'];
		DBUpdate( 'USERS'
		/*where*/	 , array( 'STAFF_ID' => User('STAFF_ID') )
		/*set*/	 , array( 'CURRENT_SCHOOL_ID' => $_REQUEST['side_school'] )
				);
	}

	if ($_REQUEST['side_syear']) {
		$_SESSION['UserSyear'] = $_REQUEST['side_syear'];
	}
	
	if ($_REQUEST['side_period']) {
		$_SESSION['UserCoursePeriod'] = $_REQUEST['side_period'];
	}
	
	if ($_REQUEST['side_mp']) {
		$_SESSION['UserMP'] = $_REQUEST['side_mp'];
	}

	if (User('PROFILE') == 'parent') {
		if ($_REQUEST['student_id'] 
		&&  $_SESSION['student_id'] != $_REQUEST['student_id']
		){
			unset($_SESSION['UserMP']);
		}
		
		if ($_REQUEST['student_id']) {
			if ($_REQUEST['student_id'] != $_SESSION['student_id']) {
				$student_changed = true;
			}	
			$_SESSION['student_id'] = $_REQUEST['student_id'];
		}
	}
/*	echo "
<script language=javascript>
parent.body.location='"
				. str_replace('&amp;','&'
							,PreparePHP_SELF($_SESSION['_REQUEST_vars']))
				. "';
</script>"; 
*/
}

if (!$_SESSION['UserSyear']) {
	$_SESSION['UserSyear'] = $DefaultSyear;
}

if (!$_SESSION['student_id'] 
&&  User('PROFILE') == 'student'
){
	$_SESSION['student_id'] = $_SESSION['STUDENT_ID'];
}

if (!$_SESSION['UserSchool'] 
&&  User('PROFILE') == 'admin' 
&& ( !User('SCHOOLS') 
 || strpos(User('SCHOOLS'), ',' . User('CURRENT_SCHOOL_ID') . ',') !== false )
){
	$_SESSION['UserSchool'] = User('CURRENT_SCHOOL_ID');
}
elseif (!$_SESSION['UserSchool'] 
&&  User('PROFILE') == 'student'
){
	$_SESSION['UserSchool'] = substr(ltrim(User('SCHOOLS'),',')
									, 0
									, strpos(ltrim(User('SCHOOLS'),',')
											,',')
									);
}
elseif (!$_SESSION['UserSchool'] 
&&  User('PROFILE') == 'teacher'
){
	$user_mp = UserMP();
	$user_syear = UserSyear();
	$user_staff_id = User('STAFF_ID');

	if ($user_mp) {
		$all_mp = GetAllMP('QTR',$user_mp);
		$_SESSION['UserSchool'] = DBGetValue("
				SELECT        cp.SCHOOL_ID as VALUE
				FROM          COURSE_PERIODS cp
				 ,            SCHOOL_PERIODS sp
				 ,            COURSES c
				WHERE         c.COURSE_ID = cp.COURSE_ID 
				 AND          cp.PERIOD_ID = sp.PERIOD_ID 
				 AND          cp.SYEAR = '$user_syear' 
				 AND          cp.TEACHER_ID = '$user_staff_id' 
				 AND          cp.MARKING_PERIOD_ID IN ($all_mp) 
				ORDER BY      sp.SORT_ORDER 
				");
		unset($all_mp);
	}
	else {
		$_SESSION['UserSchool'] = DBGetValue("
				SELECT        cp.SCHOOL_ID as VALUE
				FROM          COURSE_PERIODS cp
				 ,            SCHOOL_PERIODS sp
				 ,            COURSES c
				WHERE         c.COURSE_ID = cp.COURSE_ID 
				 AND          cp.PERIOD_ID = sp.PERIOD_ID 
				 AND          cp.SYEAR = '$user_syear' 
				 AND          cp.TEACHER_ID = '$user_staff_id' 
				ORDER BY      sp.SORT_ORDER 
				");	
	}	
	unset($user_mp);
	unset($user_syear);
	unset($user_staff_id);
}

if (( !$_SESSION['UserMP'] 
 ||  ( $_REQUEST['side_school'] 
  &&   $_REQUEST['side_school'] != $old_school ) 
 ||  ( $_REQUEST['side_syear'] 
  &&   $_REQUEST['side_syear'] != $old_syear ) ) 
&&  User('PROFILE') != 'parent'
){
	$_SESSION['UserMP'] = GetCurrentMP('QTR', DBDate(), false);
}

if (($_REQUEST['side_school'] 
 &&  $_REQUEST['side_school'] != $old_school ) 
|| ( $_REQUEST['side_syear'] 
 && $_REQUEST['side_syear'] != $old_syear )
){
	unset($_SESSION['UserPeriod']);
	unset($_SESSION['UserCoursePeriod']);
}

if ($_REQUEST['student_id'] == 'new') {
	unset($_SESSION['student_id']);
	unset($_SESSION['_REQUEST_vars']['student_id']);
	unset($_SESSION['_REQUEST_vars']['search_modfunc']);
	unset($_SESSION['_REQUEST_vars']['LO_index']);
	unset($_SESSION['student_list']);
}
// END FROM Side.php

$sep = ' &nbsp; <B><font size=+1>&middot;</font></B> &nbsp; ';

// Side.php

$bar = '';
if (User('PROFILE') == 'admin') {
	$RET = DBGet(DbQuery("
			SELECT        ID
			 ,            TITLE 
			FROM          SCHOOLS 
			WHERE         1=1 " .
	($schools ? " 
			 AND          ID IN ($schools)" : '') .
	(($_REQUEST['district'] && $_REQUEST['district'] != 'All') ? " 
			 AND          DISTRICT = '$_REQUEST[district]'" : '') . "
			ORDER BY      TITLE
			"));
	
	if (!UserSchool()) {
		$_SESSION['UserSchool'] = $RET[1]['ID'];
		DBUpdate( 'USERS'
			, array( 'STAFF_ID'          => User('STAFF_ID') )
			, array( 'CURRENT_SCHOOL_ID' => $RET[1]['ID']       )
			);
	}

	if (count($RET) > 1 
	|| $_REQUEST['district']
	){
		$bar .= $sep . "
			<select	name='side_school' 
					onChange='document.forms[0].submit();'
					style='width: 150;'>";
		
		foreach ($RET as $school) {
			$bar .= "
				<option value='$school[ID]' " . ((UserSchool() == $school['ID'])
											? 'SELECTED' 
											: '') . ">
					$school[TITLE]
				</option>";
		}
		
		$bar .= "
			</select>";
	}
}

if (User('PROFILE') == 'admin' 
||  User('PROFILE') == 'teacher'
){
	$year = date('Y');
	$bar .= $sep . "
			<select	name='side_syear'
					onChange='document.forms[0].submit();'>";
					
	for($i = $year - 5; $i <= $year + 1; $i++) {
		$bar .= "<option	value='$i'" . ((UserSyear() == $i)
											?' SELECTED'
											:'') . ">
					" . $i . ((SystemPreferences('SYEAR_TITLE') == 'YYYY-YYYY')
								? '-' . ($i + 1)
								: '') . "
				</option>";
	}
	$i = 0;
	$bar .= "
			</select>";
}

if (User('PROFILE') == 'parent') {
	$user_staff_id = User('STAFF_ID');
	$db_date = DbDate();
	
	$RET = DBGet(DBQuery("
			SELECT        sju.STUDENT_ID
			 ,            s.LAST_NAME || ', ' || s.FIRST_NAME as FULL_NAME
			 ,            se.SCHOOL_ID 
			FROM          STUDENTS s
			 ,            STUDENTS_JOIN_USERS sju
			 ,            STUDENT_ENROLLMENT se 
			WHERE         s.STUDENT_ID = sju.STUDENT_ID 
			 AND          sju.STAFF_ID = '" . User('STAFF_ID') . "' 
			 AND          se.SYEAR = $DefaultSyear
			 AND          se.STUDENT_ID=sju.STUDENT_ID 
			 AND (        '$db_date' BETWEEN se.START_DATE AND se.END_DATE 
			  OR  (       '$db_date' >= se.START_DATE 
			   AND        se.END_DATE IS NULL ) ) 
			"));
			
	unset($user_staff_id);
	unset($db_date);

	if ($_REQUEST['student_id']) {
		$_SESSION['student_id'] = $_REQUEST['student_id'];
	}
	
	if (!UserStudentID()) {
		$_SESSION['student_id'] = $RET[1]['STUDENT_ID'];
	}
	
	$bar .= $sep . "
			<select	name='student_id' 
					onChange='document.forms[0].submit();'>";
	
	if (count($RET)) {
		foreach ($RET as $student) {
			$bar .= "
				<option value='$student[STUDENT_ID]'" . 
						( (UserStudentID() == $student['STUDENT_ID'])
						? ' SELECTED'
						: '').">
					$student[FULL_NAME]
				</option>";
				
			if (UserStudentID() == $student['STUDENT_ID']) {
				$_SESSION['UserSchool'] = $student['SCHOOL_ID'];
			}
		}	
	}
	
	$bar .= "</select>";
}

if (!$_SESSION['UserMP'] 
&& ( User('PROFILE') == 'parent' 
 ||  User('PROFILE') == 'student' )
){
	$_SESSION['UserMP'] = GetCurrentMP('QTR', DBDate(), false);
}

if (User('PROFILE') == 'teacher') {
	/*  I dont know when this was commented out, 
	    so I reformatted it to be understandable
	if (UserMP())
		$QI = DBQuery("
			SELECT DISTINCT cp.PERIOD_ID
			,cp.COURSE_PERIOD_ID
			,sp.TITLE
			,sp.SHORT_NAME
			,cp.MARKING_PERIOD_ID
			,cp.DAYS
			,cp.SCHOOL_ID
			,sp.SORT_ORDER
			,c.TITLE AS COURSE_TITLE 
			FROM COURSE_PERIODS cp
			, SCHOOL_PERIODS sp
			,COURSES c 
			WHERE c.COURSE_ID=cp.COURSE_ID 
			AND cp.PERIOD_ID=sp.PERIOD_ID 
			AND cp.SYEAR='".UserSyear()."' 
			AND cp.TEACHER_ID='".User('STAFF_ID')."' 
			AND cp.MARKING_PERIOD_ID IN (".GetAllMP('QTR',UserMP()).") 
			ORDER BY sp.SORT_ORDER 
			");
	else
	*/
	$user_syear = UserSyear();
	$user_staff_id = User('STAFF_ID');
	$RET = DBGet(DBQuery("
			SELECT DISTINCT  cp.PERIOD_ID
			 ,               cp.COURSE_PERIOD_ID
			 ,               sp.TITLE
			 ,               sp.SHORT_NAME
			 ,               cp.MARKING_PERIOD_ID
			 ,               cp.DAYS
			 ,               cp.SCHOOL_ID
			 ,               sp.SORT_ORDER
			 ,               c.TITLE AS COURSE_TITLE 
			FROM             COURSE_PERIODS cp
			 ,               SCHOOL_PERIODS sp
			 ,               COURSES c 
			WHERE            c.COURSE_ID = cp.COURSE_ID 
			 AND             cp.PERIOD_ID = sp.PERIOD_ID 
			 AND             cp.SYEAR = '$user_syear' 
			 AND             cp.TEACHER_ID = '$user_staff_id' 
			ORDER BY         sp.SORT_ORDER	
			"));
	unset($user_syear);
	unset($user_staff_id);

	$bar .= $sep . "
			<select	name='side_period' 
					onChange='document.forms[0].submit();' 
					style='width: 150;'>";
					
	$time = strtotime(DBDate('postgres'));
	
	foreach ($RET as $i => $period) {
		if ($period['MARKING_PERIOD_ID'] != '0' 
		&&  GetMP($period['MARKING_PERIOD_ID'], 'START_DATE') 
		&&  GetMP($period['MARKING_PERIOD_ID'], 'END_DATE') 
		&& ( $time < strtotime(GetMP($period['MARKING_PERIOD_ID'], 'START_DATE')) 
		 ||  $time > strtotime(GetMP($period['MARKING_PERIOD_ID'], 'END_DATE')) )
		){
		// do nothing?
		}
		else {
			if (!UserPeriod()) {
				$_SESSION['UserPeriod'] = $RET[$i]['PERIOD_ID'];
			}	
			
			if (!UserCoursePeriod()) {
				$_SESSION['UserCoursePeriod'] = $RET[$i]['COURSE_PERIOD_ID'];
			}
			
			$bar .= "
				<option	value='$period[COURSE_PERIOD_ID]'" . 
						((UserCoursePeriod() == $period['COURSE_PERIOD_ID']) 
						 ? ' SELECTED'
						 : '').">
					$period[SHORT_NAME]" . 
						(($period['MARKING_PERIOD_ID'] != '0')
						 ? ' ' . GetMP($period['MARKING_PERIOD_ID'], 'SHORT_NAME')
						 : '') .
						((strlen($period['DAYS']) < 5)
						 ? ' ' . $period['DAYS']
						 : '') . "
					- $period[COURSE_TITLE]
				</option>";
				
			if (UserCoursePeriod() == $period['COURSE_PERIOD_ID']) {
				if ($period['SCHOOL_ID'] != UserSchool()) {
					unset($_SESSION['UserMP']);
				}
				$_SESSION['UserSchool'] = $period['SCHOOL_ID'];
				$_SESSION['UserPeriod'] = $period['PERIOD_ID'];
			}
			unset($RET[$i]);
		}
	}

	if (count($RET)) {
		if (!UserPeriod()) {
			$_SESSION['UserPeriod'] = $RET[1]['PERIOD_ID'];
		}
		
		if (!UserCoursePeriod()) {
			$_SESSION['UserCoursePeriod'] = $RET[1]['COURSE_PERIOD_ID'];
		}
		
		$bar .= '
				<optgroup label="'._('Inactive Courses').'">';
				
		foreach ($RET as $period) {
			$bar .= "
					<option value='$period[COURSE_PERIOD_ID]'" . 
							((UserCoursePeriod() == $period['COURSE_PERIOD_ID'])
							 ? ' SELECTED'
							 : '').">
						$period[SHORT_NAME]" . 
						(($period['MARKING_PERIOD_ID'] != '0')
						 ? ' ' . GetMP($period['MARKING_PERIOD_ID'], 'SHORT_NAME')
						 : '') .
						((strlen($period['DAYS']) < 5)
						 ? ' ' . $period['DAYS']
						 : '' ) . "
						- $period[COURSE_TITLE]
					</option>";
					
			if (UserCoursePeriod() == $period['COURSE_PERIOD_ID']) {
				if ($period['SCHOOL_ID'] != UserSchool()) {
					unset($_SESSION['UserMP']);
				}
				$_SESSION['UserSchool'] = $period['SCHOOL_ID'];
				$_SESSION['UserPeriod'] = $period['PERIOD_ID'];
			}
		}
		$bar .= '
				</optgroup>';
	}
	$bar .= "
			</select>";
}

if (User('PROFILE') == 'admin' 
||  User('PROFILE') == 'teacher'
){
	$bar .= $sep . "
			<select	name='side_mp'
					onChange='document.forms[0].submit();'>";
					
	$user_syear = UserSyear();
	$user_school = UserSchool();
	$RET = DBGet(DBQuery("
			SELECT        MARKING_PERIOD_ID
			 ,            TITLE 
			FROM          SCHOOL_QUARTERS 
			WHERE         SCHOOL_ID = '$user_school' 
			 AND          SYEAR = '$user_syear' 
			ORDER BY      SORT_ORDER
			"));
	unset($user_syear);
	unset($user_school);
			
	if (count($RET)) {
		if (!UserMP()) {
			$_SESSION['UserMP'] = $RET[1]['MARKING_PERIOD_ID'];
		}
		
		$mp_selected = false;
		
		foreach ($RET as $quarter) {
			if (UserMP() == $quarter['MARKING_PERIOD_ID']) {
				$mp_selected = true;
				$selected = 'SELECTED';
			}
			else {
				$selected = '';
			}
			
			$bar .= "
				<option	value='$quarter[MARKING_PERIOD_ID]' $selected>
					$quarter[TITLE]
				</option>";
		}
		
		if (!$mp_selected) {
			$_SESSION['UserMP'] = $RET[1]['MARKING_PERIOD_ID']; 
			// if for some reason, the current UserMP is not one that's available, 
			// select the first one.  
			// this probably indicates a bug in Side.php
		}
	}
	$bar .= "
			</select>";
}

// End Side.php
$tmp_REQUEST = $_SESSION['_REQUEST_vars'];
unset($tmp_REQUEST['values']);
unset($tmp_REQUEST['search_modfunc']);
unset($tmp_REQUEST['tables']);
$tmp_REQUEST['modname'] = $_REQUEST['modname'];
$tmp_REQUEST['student_id'] // =
		= $tmp_REQUEST['staff_id'] 
		= $tmp_REQUEST['side_school'] 
		= $tmp_REQUEST['side_mp'] 
		= $tmp_REQUEST['side_period'] 
		= $tmp_REQUEST['side_syear'] 
		= $tmp_REQUEST['LO_index'] 
		= '';
	
if ($student_changed === true 
&& strpos($_REQUEST['modname'], 'StudentGBGrades.php') !== false
){
	unset($tmp_REQUEST['modname']);
	unset($tmp_REQUEST['course_period_id']);
	unset($tmp_REQUEST['search_modfunc']);
	$_REQUEST['modname'] = 'misc/Portal.php';
}
$tmp_PHP_SELF = PreparePHP_SELF($tmp_REQUEST);

if ($_REQUEST['top_deleted_student'] == 'true') {
	unset($_SESSION['student_id']);
	unset($_REQUEST['top_deleted_student']);
	unset($_SESSION['_REQUEST_vars']['top_deleted_student']);
	unset($_REQUEST['student_id']);
}
elseif ($_REQUEST['top_deleted_user'] == 'true') {
	unset($_SESSION['staff_id']);
	unset($_REQUEST['top_deleted_user']);
	unset($_SESSION['_REQUEST_vars']['top_deleted_user']);
	unset($_REQUEST['staff_id']);
}

// TO ASSIGN THE SESSION STUDENT ID IF THE STUDENT HAS CHANGED 
if ($_REQUEST['student_id'] 
&& User('PROFILE') != 'parent' 
&& User('PROFILE') != 'student' 
&& $_REQUEST['student_id'] != 'new' 
&& $_REQUEST['search_modfunc'] != 'list'
){
	$_SESSION['student_id'] = $_REQUEST['student_id'];
}
elseif ($_REQUEST['staff_id'] 
&& User('PROFILE') == 'admin' 
&& $_REQUEST['staff_id'] != 'new' 
&& $_REQUEST['search_modfunc'] != 'list'
){
	$_SESSION['staff_id'] = $_REQUEST['staff_id'];
}

if ( ( UserStudentID() 
 ||    $_REQUEST['student_id'] ) 
&&  User('PROFILE') != 'parent' 
&&  User('PROFILE') != 'student' 
&&  $_REQUEST['student_id'] != 'new' 
&&  $_REQUEST['search_modfunc'] != 'list'
){
	$user_student_id = UserStudentID();
	$RET = DBGet(DBQuery("
			SELECT        FIRST_NAME
			 ,            LAST_NAME
			 ,            MIDDLE_NAME
			 ,            NAME_SUFFIX 
			FROM          STUDENTS 
			WHERE         STUDENT_ID = '$user_student_id'
			"));
	unset($user_student_id);

	$bar .= $sep . "
			<a	href='$tmp_PHP_SELF&student_id=new&top_deleted_student=true'>
				<img	src='assets/x.gif' 
						height='14' 
						border='0' />
			</a>
			" .	DrawTab($RET[1]['FIRST_NAME'] . '&nbsp;' . 
						(( $RET[1]['MIDDLE_NAME'] )
						 ? $RET[1]['MIDDLE_NAME'] . ' '
						 : '') 
						 . $RET[1]['LAST_NAME'] 
						 . '&nbsp;' 
						 . $RET[1]['NAME_SUFFIX']
						, 'Modules.php?modname=Students/Student.php'
						, 'FFFFFF'
						, '333333'
						, '_circle'
						,array( 'tabcolor'  => Preferences('HIGHLIGHT')
							  , 'textcolor' => 'FFFFFF' )
						, 'style="display:inline;"'
						);
}
elseif ( ( UserStaffID() || $_REQUEST['staff_id'] ) 
&& User('PROFILE') == 'admin' 
&& $_REQUEST['staff_id'] != 'new'
){
	$user_syear = UserSyear();
	$user_staff_id = UserStaffID();
	$RET = DBGet(DBQuery("
			SELECT        FIRST_NAME
			 ,            LAST_NAME 
			FROM          USERS 
			WHERE         STAFF_ID = '$user_staff_id' 
			 AND          SYEAR = '$user_syear'
			"));			
	unset($user_syear);
	unset($user_staff_id);

	$bar .= $sep . "
		<a		href='" . $tmp_PHP_SELF . "&staff_id=new&top_deleted_user=true'>
			<img	src='assets/x.gif'
			 		height='14'
					border='0' />
		</a>" .
		DrawTab($RET[1]['FIRST_NAME']
					. '&nbsp;'
					. ($RET[1]['MIDDLE_NAME']
						? $RET[1]['MIDDLE_NAME'] . ' '
						: '')
					. $RET[1]['LAST_NAME']
					. '&nbsp;'
					. $RET[1]['NAME_SUFFIX']
				, 'Modules.php?modname=Users/User.php'
				, 'FFFFFF'
				, '333333'
				, '_circle'
				, array(  'tabcolor'  => Preferences('HIGHLIGHT')
						, 'textcolor' => 'FFFFFF' )
				,'style="display:inline;"'
				);
}

echo "
	<table	width='100%'
			bgcolor='#FFFFFF'>
		<tr>
			<td		valign='middle'>
				<a		href='index.php'
				 		target='_top'>" .
					DrawPNG(  'themes/' . Preferences('THEME') . '/logo.png'
							, 'border=0 width=154 height=34'
							) . "
				</a>
			</td>
			<td		valign='top' 
					align='" . ALIGN_RIGHT . "'>";
					
if (User('PROFILE')!='student')
{
	$user_name = User('NAME');
	echo "
				<small>
					&nbsp;
					<b>$user_name</b>
				</small>'
				<form	action='" . PreparePHP_SELF($tmp_REQUEST) . "' 
						method='POST'
						style='display:inline;'>
					$bar
				</form>";
	unset($user_name);
}

echo "
			</td>
		</tr>
	</table>
	
<!-- System Information -->	
	
	<table	border='0' 
			cellpadding='0'
			cellspacing='0'
			width='100%'>
		<tr>";
		
require_once($staticpath . 'Menu.php');

foreach ($_FOCUS['Menu'] as $modcat => $programs) {
	if ($programs[$_REQUEST['modname']]) {
		$_SESSION['modcat'] = $modcat;
	}
	$next_modcat[$prev_modcat] = $modcat;
	$prev_modcat = $modcat;
}

$after_divs = "after_reports = getXPos('after_Reports');\n";

foreach ($_FOCUS['Menu'] as $modcat => $programs) {
	if (count($_FOCUS['Menu'][$modcat])) {
		$keys = array_keys($_FOCUS['Menu'][$modcat]);
		$menu = false;
		foreach ($keys as $key_index => $file) {
			if (!is_numeric($file)) {
				$menu = true;
			}
		}
		
		if (!$menu) {
			continue;
		}

		if ($modcat == $_SESSION['modcat']) {
			echo "
			<td		width='2'></td>
			<td		align='center'>";
		}
		else {
			echo "
			<td		width='2'></td>
			<td 	bgcolor='#DFDFDF'
					align='center' 
					height='22'>";
		}

		echo "
				<a		style='cursor:pointer;' 
						onclick='openModcat(\"" . $modcat . "\");'>
					<div	id='button_" . $modcat . "' 
							style='background-color:transparent;' 
							onmouseover='openRollover(\"" . $modcat . "\");'>";
							
		if (User('PROFILE') == 'student' 
		&&  $modcat == 'Your_Information'
		){
			$image = 'Students';
		}
		else {
			$image = $modcat;
		}
		
		switch($modcat) {
			case 'School_Setup':
				$localized_modcat = _('School Setup');
			break;
			
			case 'Students':
				$localized_modcat = _('Students');
			break;
			
			case 'Users':
				$localized_modcat = _('Users');
			break;
			
			case 'Grades':
				$localized_modcat = _('Grades');
			break;

			case 'School_Information':
				$localized_modcat = _('School Information');
			break;

			case 'My_Information':
				$localized_modcat = _('My Information');
			break;

			case 'My_Child':
				$localized_modcat = _('My Child');
			break;
			
			case 'Classes_&_Grades':
				$localized_modcat = _('Classes & Grades');
			break;
			
			case 'Attendance':
				$localized_modcat = _('Attendance');
			break;

			case 'Eligibility':
				$localized_modcat = _('Eligibility');
			break;
			
			case 'Discipline':
				$localized_modcat = _('Discipline');
			break;
			
			case 'Billing':
				$localized_modcat = _('Billing');
			break;

			case 'Reports':
				$localized_modcat = _('Reports');
			break;
			
			default:
				$localized_modcat = str_replace('_', ' ', $modcat);
			break;
		}

		echo "
						" . DrawPNG('icons/' . $image . '.png'
									, 'border=0'
									) . "
						<small>
							<b>$localized_modcat</b>
						</small>
					</div>
				</a>
			</td>";

		$menu_divs .= "
				`<div	id='menu_hidden$modcat'
						style='visibility: hidden;
								position: absolute;
								left: 10px;
								top: 69px;'>
					<table	background='assets/bg.gif' 
							cellpadding='2'
							cellspacing='0' 
							style='border-style:solid solid solid solid;
									border-width:1;'>";
									
		foreach ($keys as $key_index => $file) {
			$title = $_FOCUS['Menu'][$modcat][$file];
			if (substr($file,0,7) == 'http://') {
				$menu_divs .= "
					<tr>
						<td		onclick='document.location.href=\"$file\"' 
								onmouseover='this.style.backgroundColor=\"#"
												.Preferences('HIGHLIGHT')
												."\";" 
											. "this.style.color=\"white\";'
								onmouseout='this.style.cssText="
											. "\"background-color:transparent;"
												. "color:black;\";'>
							<a		style='cursor:pointer;'>
								&rsaquo;
								$title
							</a>
						</td>
					</tr>";
				}
			elseif (!is_numeric($file) 
			&& $file == $_REQUEST['modname']
			){
				$menu_divs .= "
					<tr>
						<td		onclick='document.location.href="
											. "\"Modules.php?modname=$file\"' 
								onmouseover='this.style.backgroundColor=\"#"
													. Preferences('HIGHLIGHT')
													. "\";" 
											. "this.style.color=\"white\";' 
								onmouseout='this.style.cssText="
											. "\"background-color:transparent;"
												. "color:black;\";'>
							<a		style='cursor:pointer;'>
								<b>
									&rsaquo;
									$title
								</b>
							</a>
						</td>
					</tr>";
			}
			elseif (!is_numeric($file)) {
				$menu_divs .= "
					<tr>
						<td		onclick='document.location.href="
										. "\"Modules.php?modname=$file\"' 
								onmouseover='this.style.backgroundColor=\"#"
													. Preferences('HIGHLIGHT')
													. "\";"
											. "this.style.color=\"white\";'
								onmouseout='this.style.cssText="
											. "\"background-color:transparent;"
												. "color:black;\";'>
							<a		style='cursor:pointer;'>
								<b>
									&rsaquo;
								</b>
								$title
							</a>
						</td>
					</tr>";
			}
			elseif ($keys[$key_index+1] 
			&& !is_numeric($keys[$key_index+1])
			){
				$menu_divs .= "
					<tr>
						<td		colspan='2'
								height='3'>
						</td>
					</tr>
					<tr>
						<td		colspan='2'>
							&nbsp; 
							<b>$title</b>
						</td>
					</tr>";
			}
		}
		
		$menu_divs .= "
					<tr>
						<td		colspan='2'>
							<img	src='assets/pixel_trans.gif' 
									height='1' 
									width='200' />
						</td>
					</tr>";
		
//		if (true){

		$menu_divs .= "
				</table>
			</div>";
			
		$after_divs .= "
				pos = getXPos('button_$modcat');
				if ((pos+208)>after_reports)
				";
				
		if ($next_modcat[$modcat]) {
			$after_divs .= "pos = getXPos('button_" 
									. $next_modcat[$modcat] . "')-208;";
		}
		else {
			$after_divs .= "pos = after_reports-208;";
		}
		
		$after_divs .= "document.getElementById('menu_hidden$modcat')
								.style.left=pos.toString()+'px';";

//		}
//		else {
//			$menu_divs .= "<TR><TD colspan=2>
//							<IMG SRC=assets/pixel_trans.gif width=200 height=1>
//			</TD></TR></TABLE></DIV>";
//			$after_divs .= "document.getElementById('menu_hidden$modcat')
//								.style.left=(after_reports-208);";
//		}
	}
}

if (count($_FOCUS['Menu']) < 6) {
	echo "
			<td		align='" . ALIGN_RIGHT . "' 
					width='40%'
					bgcolor='#DFDFDF'>
				<div	id='after_Reports'
						style='width:30;'>
				</div>
			</td>";
}
elseif (count($_FOCUS['Menu']) < 8) {
	echo "
			<td		align='" . ALIGN_RIGHT . "'
					width='25%'
					bgcolor='#DFDFDF'>
				<div	id='after_Reports'>
				</div>
			</td>";
}
else {
	echo "
			<td		align='" . ALIGN_RIGHT . "'>
				<div	id='after_Reports'>
				</div>
			</td>";
}

echo "
		</tr>
	</table>
	<script language='javascript'>
		function positionMenus() {
			$after_divs 
		}
	</script>";
$after_divs = '';
// $menu_divs is printed later so the page loads before the menu positions are calculated
?>